﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SuperSocialShare.Error;
using SuperSocialShare.Clients;
using SuperSocialShare;
using System.Windows.Resources;
using System.IO;

namespace SuperSocialShareTest
{
    public partial class SharePage : PhoneApplicationPage
    {
        public SharePage()
        {
            InitializeComponent();
            string title = string.Empty;
            switch (App.SocialType)
            {
                case SocialType.Sina:
                    title = "分享到新浪微博";
                    break;
                case SocialType.Renren:
                    title = "分享到人人网";
                    break;
                case SocialType.Tencent:
                    title = "分享到腾讯微博";
                    break;
                case SocialType.KaiXin:
                    title = "分享到开心网";
                    break;
                case SocialType.SoHu:
                    title = "分享到搜狐微博";
                    break;
                case SocialType.NetEase:
                    title = "分享到网易微博";
                    break;
                default:
                    break;
            }
            ShareTitle.Text = title;
            App.CurrentClient.ShareCompleted += CurrentClient_ShareCompleted;
            if (!string.IsNullOrEmpty(App.ShareText))
                ShareTextBox.Text = App.ShareText;
        }

        void CurrentClient_ShareCompleted(object sender, AsyncEventArgs e)
        {
            string tip = string.Empty;
            if (e.Error == null)//分享成功 
            {
                tip = "分享成功！";
            }
            else
            {
                tip = "分享失败，error:" + e.Error.Message;
            }

            Dispatcher.BeginInvoke(() =>
            {
                ProgressIndicator indicator = SystemTray.GetProgressIndicator(this);
                indicator.IsVisible = false;
                MessageBox.Show(tip);
            });
        }


        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            if (ShareTextBox.Text.Length > 0)
            {
                ProgressIndicator indicator = SystemTray.GetProgressIndicator(this);
                indicator.IsVisible = true;
                if (ShareImageOption.IsChecked == true)
                {
                    StreamResourceInfo resInfo = Application.GetResourceStream(new Uri("Assets/00141832.jpg", UriKind.Relative));
                    if (resInfo != null && resInfo.Stream != null)
                    {
                        using (var stream = resInfo.Stream)
                        {
                            byte[] imgData = new byte[(int)stream.Length];
                            BinaryReader reader = new BinaryReader(stream);
                            reader.Read(imgData, 0, imgData.Length);
                            App.CurrentClient.ShareTextWithImage(ShareTextBox.Text, imgData);
                        }
                    }
                }
                else
                    App.CurrentClient.ShareText(ShareTextBox.Text);
            }
        }

    }
}